package water.utils;


import app.nbdevices.utils.ConvertUtil;
import app.utils.TimeUtils;
import com.mess.warningmess.Bean4WarnMessInfo;
import com.mess.warningmess.WarningMass;
import water.entity.WaterCycleReport;
import water.modle.codecModle.WaterReport;
import water.modle.revModle.RevWaterReportData;


import javax.persistence.criteria.CriteriaBuilder;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 此类用来解释水表上报数据解释器
 */
public class WaterExplain {
    /**
     * 解释周期上报数据
     * @param waterReport
     * @return
     */


    public static WaterCycleReport exEleCycle(WaterReport waterReport){

        Long time=LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));

        WaterCycleReport waterCycleReport=new WaterCycleReport();

//        waterCycleReport.setAddrId(revWaterReportData.getMeterAddr());
//        waterCycleReport.setWaterCmdCode(revWaterReportData.getCommand());
        waterCycleReport.setWaterPerHourData(getLogicAddr(waterReport.getLogicAddr()));
        waterCycleReport.setWaterData((double)explainHex(waterReport.getCurrentData())/100);
        waterCycleReport.setWaterCycleInterval(explainHex(waterReport.getInterval()));
        waterCycleReport.setWaterStatus(Byte.toUnsignedInt(waterReport.getStatus()));
        waterCycleReport.setWaterRssi(Byte.toUnsignedInt(waterReport.getRssi()));
        waterCycleReport.setWaterCycleTime(explainTime(waterReport.getTime()));
        waterCycleReport.setReportTime(time);
        //     waterCycleReport.setWaterCacheDate(explainYMD(waterReport.getCacheDataTime()));
   //     waterCycleReport.setWaterPerHourData(ConvertUtil.getBase64Codec().encodeAsString(waterReport.getPerHourData()));
        int[] res=getDayMonYear(time);
        waterCycleReport.setDay(res[0]);
        waterCycleReport.setMon(res[1]);
        waterCycleReport.setYear(res[2]);
        return waterCycleReport;
    }
    public static void main(String[] args) {

//        long timeLong = TimeUtils.getNowDateLong();
//        Long tempTime = 4l;
//        Long endTime=timeLong+ tempTime * 3600l * 1000l;
//        System.out.println("dddd");
//    int p[]=getDayMonYear(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
//    System.out.println(p[0]);
//    Long time=LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
//   int[] rse= getDayMonYear(time);
//        for(int d:rse){
//            System.out.println(d);
//        }
//        String res="AAAAAAEAACAZERGXiYYRGSZgB4ggZQAAAAAgBBMXUCIFoM8AAAAAlFGgiQzmJgzdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
//        String res1="AAAAAAEAACAZERGZiYYRGSZgB4gnJwAPHYggBCMRMgAFoM8AAAAAlFGgiQzmOAfdAAAAAAAAAAAAAAAAAA8hcAAPIXAADyFwAA8hcAAPIQwADyEMAA8hDAAPIKgADyCoAA8gqAAPIEQADyBEAA8gRAAPH+AADx/gAA8f4AAPH+AADx/gAA8ffAAPH3wADx98AA8fGAAPHxgADx8Y";
//        byte[] decode = ConvertUtil.getBase64Codec().decode(res1);
//        for(byte b:decode){
//            System.out.print(Integer.toHexString(b&0xff)+" ");
//        }
//        WaterReport waterReport = WaterDataDecode.WaterReportDataDecode(res1);
//        byte[] currentData = waterReport.getCurrentData();
//        int i = explainHex(currentData);
//        System.out.println();
//        for(byte b:currentData){
//            System.out.print(Integer.toHexString(b&0xff)+" ");
//        }
//        System.out.println("读书："+i);
//        exEleCycle(waterReport);

//            String u = "那曲电信分公司生活区  5-10";
//        int[] ints = sortNum(u);
//        for(int i=0;i<ints.length;i++){
//            System.out.println(ints[i]);
//        }
        Bean4WarnMessInfo info = new Bean4WarnMessInfo("CLS00001763", "水表", "那曲电信分公司生产区1-2/水表", "一般故障", "李将", "13325874078", "电池缺电负");
        		WarningMass.sendMess(info);

    }
    public static int[] sortNum(String u){
        Pattern p = Pattern.compile("\\d{1,}");
        Matcher m = p.matcher(u);
        int [] num=new int[2];
        int i=0;
        while (m.find()) {
            num[i++]=Integer.parseInt(m.group());
        }
        return num;
    }
    public static String getLogicAddr(byte[] data){
        StringBuffer res=new StringBuffer();
        for(int i=2;i<data.length;i++){
            String s = ConvertUtil.byteToString(data[i]);
            res.append(s);

        }
       // System.out.println();
        return res.toString();
    }
//    public static int[] getDayMonYear(Long time){
//        final int[] days={31,59,90,120,151,181,212,243,273,304,334,365};
//
//        int[] res=new int[3];
//        Long t=(time%nd)/nd;
//        int day=new Long((time-time%nd)/nd).intValue()+1;
//
//        day=day-(365*3+1);
//        int nowDay=day%(365*4+1);
//
//        int year=1973+day/(365*4+1)*4+nowDay/365;
//        res[2]=year;
//        int nowYearDay=nowDay%365;
//            if(nowYearDay<=31){
//                res[1]=1;
//            }else  if(year % 4== 0 && year%100!=0||year%400==0){
//                for (int i = 1; i < days.length; i++) {
//                    if (nowYearDay <= (days[i]+1)) {
//                        res[1]=i+1;
//                        break;
//                    }
//                }
//            }else {
//                for (int i = 1; i < days.length; i++) {
//                    if (nowYearDay <= days[i]) {
//                        res[1]=i+1;
//                    }
//                }
//            }
//            res[1]=res[2]*10+res[1];
//        return res;
//    }

    public static int[] getDayMonYear(Long time){
    //    final Long nd = 24l * 60l * 60l;
        LocalDateTime time1 = LocalDateTime.ofEpochSecond(time, 0, ZoneOffset.ofHours(8));
        String dd=time1.format(ConvertUtil.getFormatter());
        int res[]=new int[3];
//        int day=new Long((time-time%nd)/nd).intValue()+1;
//        if(day%2==0){
//            res[0]=Integer.parseInt(dd)-1;
//        }else{
//            res[0]=Integer.parseInt(dd);
//        }
        res[0]=Integer.parseInt(dd);
        res[1]=Integer.parseInt(dd.substring(0,6));
        res[2]=Integer.parseInt(dd.substring(0,4));
        return res;
    }



    /**
     * 解释年月日时分秒
     * @param a
     * @return
     */
    private static Long explainTime(byte[] a){
        if(a==null||a.length!=6){
            return 0l;
        }
        int year=Integer.parseInt("20"+ConvertUtil.byteToString(a[0]));
        int mounth=Integer.parseInt(ConvertUtil.byteToString(a[1]));
        int day=Integer.parseInt(ConvertUtil.byteToString(a[2]));
        int hour=Integer.parseInt(ConvertUtil.byteToString(a[3]));
        int minute=Integer.parseInt(ConvertUtil.byteToString(a[4]));
        int second=Integer.parseInt(ConvertUtil.byteToString(a[5]));
        LocalDateTime time= LocalDateTime.of(year,mounth,day,hour,minute,second);
        return time.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 解释Hex
     * @param r
     * @return
     */
    private static int explainHex(byte[] r){
        int read=0;
        int carry=1;
        for(int i=r.length-1;i>=0;i--){
            int tem=Byte.toUnsignedInt(r[i]);
            tem*=carry;
            carry*=(16*16);
            read+=tem;
        }
        return read;
    }

//    /** 设备不支持  暂时无用
//     * 解释年月日
//     * @param y
//     * @return
//     */
//    private static Date explainYMD(byte[] y){
//        if(y==null||y.length!=3){
//            return null;
//        }
//        StringBuffer s=new StringBuffer();
//        s.append("20");
//        s.append(ConvertUtil.byteToString(y[0]));
//        s.append("-"+ConvertUtil.byteToString(y[1]));
//        s.append("-"+ConvertUtil.byteToString(y[2]));
//        System.out.println(s);
//        LocalDate date=LocalDate.parse(s.toString(),ConvertUtil.getFormatter());
//        return Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
//    }





}
